import axios from 'axios';

// Create an instance of axios
const http = axios.create({
  baseURL: 'https:/v3pz.itndedu.com/v3pz', // Base URL for the API
  timeout: 10000, // Request timeout
  headers: {
    "terminal": "h5"
  }
});

// Request interceptor
http.interceptors.request.use(
  config => {
    // Add any custom headers or tokens here
    // For example: config.headers['Authorization'] = 'Bearer ' + token;
    const token = localStorage.getItem('h5_token');
    const whiteUrl = ['/login']
    if (token && !whiteUrl.includes(config.url)) {
      config.headers['h-token'] = token
    }
    return config;
  },
  error => {
    // Handle request error here
    console.error(error);
    return Promise.reject(error);
  }
);

// Response interceptor
http.interceptors.response.use(
  //对接口异常的数据需要给用户提示
  response => {
    if (response.data.code === -1) {
      //暂时保留逻辑，后续根据实际情况修改


    }
    if (response.data.code === -2) {
      localStorage.removeItem('h5_token')
      localStorage.removeItem('h5_userInfo')
      //将浏览器跳转到当前页面的根路径

      window.location.href = window.location.origin
    }
    // Handle response data here
    return response;
  }
  ,
  error => {
    // Handle response error here
    console.error(error);
    return Promise.reject(error);
  }
);

export default http;